package hauwei.year2025.month4;

import java.util.*;

public class UpdateTime {
    public static ArrayList<String[]> list;
    public static int max=Integer.MIN_VALUE;
    public static TreeSet<String> res=new TreeSet<>();
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        list = new ArrayList<>(n);
        int []visited = new int[n];
        for (int i = 0; i < n; i++) {
            String s = sc.nextLine();
            list.add(s.split(" "));
        }
        sc.close();
        for (int i = 0; i < n; i++) {
            dfs(list.get(i)[0],i,visited,0);
        }
        Iterator<String> iterator = res.iterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next());
            if(iterator.hasNext()){
                System.out.print(" ");
            }
        }
    }
    public static void dfs(String start,int index,int[]visited,int sum){
        if(visited[index]==1){
            return;
        }
        visited[index]=1;
        String next = list.get(index)[1];
        if(next.equals("NA")){
            if(max<sum){
                max=sum;
                res.clear();
                res.add(start);
            }else if(max==sum){
                res.add(start);
            }
        }
        for(int i=0;i<list.size();i++){
            if(visited[i]==0){
                String s = list.get(i)[0];
                if(s.equals(next)){
                    dfs(start,i,visited,sum+1);
                }
            }
        }
        visited[index]=0;
    }
}
